/**
 * 
 */
package de.wesemann.mediahandbook.le.util;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;

import android.util.Log;
import de.wesemann.jdedun.entities.Paging;
import de.wesemann.jdedun.entities.mediahandbook.companies.MediahandbookCompaniesContainer;
import de.wesemann.jdedun.entities.mediahandbook.companies.MediahandbookCompany;
import de.wesemann.jdedun.exceptions.PageNotFoundException;
import de.wesemann.jdedun.implementation.mediahandbook.MediahandbookCompaniesReq;

/**
 * @author spinner0815
 * 
 */
public class CompanyUtil {
	private static final String LOG_TAG = CompanyUtil.class.getSimpleName();
	private static MediahandbookCompaniesReq COMPANY_REQ = new MediahandbookCompaniesReq();
	private static MediahandbookCompaniesContainer mCompCont = null;
	private static boolean mFirstLoad = true;

	public static List<MediahandbookCompany> getCompanies() throws PageNotFoundException {

		if (mFirstLoad) {
			Log.d(LOG_TAG, "First laod");
			mCompCont = COMPANY_REQ.getFirstN(30);
			if (mCompCont == null) {
				throw new PageNotFoundException("couldn't laod");
			}
			mFirstLoad = false;
			return mCompCont.getData();
		} else {
			if (mCompCont == null) {
				throw new PageNotFoundException("couldn't laod");
			}
			mCompCont = COMPANY_REQ.getNext(mCompCont.getPaging());
			return mCompCont.getData();
		}
	}

	public static MediahandbookCompaniesContainer getFirstCompanies(int n) throws FileNotFoundException {
		return COMPANY_REQ.getFirstN(n);
	}

	public static MediahandbookCompaniesContainer getNextCompanies(Paging<MediahandbookCompany> page)
			throws PageNotFoundException, FileNotFoundException {
		return COMPANY_REQ.getNext(page);
	}

	public static MediahandbookCompaniesContainer searchCompaniesByNameWithLimit(int limit, String compName)
			throws FileNotFoundException {
		HashMap<String, String> searchTuple = new HashMap<String, String>();
		searchTuple.put("name", compName);
		return COMPANY_REQ.searchWithLimit(limit, searchTuple);
	}

	public static List<MediahandbookCompany> searchCompaniesByName(String compName) throws FileNotFoundException {

		HashMap<String, String> searchTuple = new HashMap<String, String>();
		searchTuple.put("name", compName);
		List<MediahandbookCompany> foundcompanies = COMPANY_REQ.search(searchTuple);
		return foundcompanies;
	}

	public static List<MediahandbookCompany> searchCompanyById(int id) throws FileNotFoundException {

		HashMap<String, String> searchTuple = new HashMap<String, String>();
		searchTuple.put("id", String.valueOf(id));
		List<MediahandbookCompany> foundcompanies = COMPANY_REQ.search(searchTuple);
		return foundcompanies;
	}

	public static List<MediahandbookCompany> searchCompanyBySubMarketId(int id) throws FileNotFoundException {

		HashMap<String, String> searchTuple = new HashMap<String, String>();
		searchTuple.put("sub_market_id", String.valueOf(id));
		List<MediahandbookCompany> foundcompanies = COMPANY_REQ.search(searchTuple);
		return foundcompanies;
	}

	public static MediahandbookCompaniesContainer searchCompanyBySomethingWithLimit(int limit, String searchKey,
			String searchValue) throws FileNotFoundException {
		HashMap<String, String> searchTuple = new HashMap<String, String>();
		searchTuple.put(searchKey, searchValue);
		return COMPANY_REQ.searchWithLimit(limit, searchTuple);

	}

	public static List<MediahandbookCompany> searchCompanyBySomething(String searchKey, String searchValue)
			throws FileNotFoundException {
		HashMap<String, String> searchTuple = new HashMap<String, String>();
		searchTuple.put(searchKey, searchValue);
		return COMPANY_REQ.search(searchTuple);

	}
}
